'use strict';
import React, {Component} from 'react';
import {
    StyleSheet,
    View,
    Text,
    Image,
    TouchableOpacity,
} from 'react-native';
import Global from '../Commom/GlobalConst';
import Toast from '../Utils/toast';
import {UserLogout} from '../Action/UserAction';
import FeedbackPage from './FeedbackPage';
import AboutUsPage from './AboutUsPage';
var {NativeModules} = require('react-native');
// import * as CacheManager from 'react-native-http-cache';

export default class SettingPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            cachesize: '10M',
        }
    }

    componentDidMount() {
        this.getData();
    }

    async getData() {
        try {
            // this.setState({
            //     'cachesize': (await CacheManager.getCacheSize() / 1024 / 1024).toFixed(2).toString() + 'M',
            // });
        } catch (err) {
            alert(err.message);
        }
    }

    LogOutAction(number) {
        switch (number) {
            case 0:
                Toast.show('清除缓存成功');
                // CacheManager.clearCache();
                this.setState({
                    'cachesize': '0M',
                });
                break;
            case 1:
                this.props.navigator.push({
                    id: 'AboutUsPage',
                    component: AboutUsPage,
                    passProps: {},
                });
                break;
            case 2:
                this.props.navigator.push({
                    id: 'FeedbackPage',
                    component: FeedbackPage,
                    passProps: {},
                });
                break;
            case 3:
                const {dispatch} = this.props;
                dispatch(UserLogout());
                Toast.show('退出成功');
                this.BackAction();
                break;
        }
    }

    BackAction() {
        this.props.navigator.pop();
    }

    render() {
        return (
            <View style={styles.container}>
                <View style={{width:Global.Const.size.width,height:65,backgroundColor:'white'}}>

                    <View style={{marginTop:24,alignItems:'center'}}>

                        <View style={{width:21,height:21,position:'absolute',left:10,top:3}}>
                            <TouchableOpacity activeOpacity={0.5}
                                              onPress={()=>this.BackAction()}>
                                <Image style={{resizeMode:'contain',width:21,height:21}}
                                       source={require('../../Image/icon_back.png')}></Image>
                            </TouchableOpacity>
                        </View>
                        <Text style={{fontSize:20,color:Global.FontColor.DimGray}}>设置</Text>
                    </View>
                </View>
                <TouchableOpacity activeOpacity={0.5}
                                  onPress={()=>this.LogOutAction(0)}>
                    <View
                        style={{marginTop:8,backgroundColor:'white',width: Global.Const.size.width,height: 45,borderBottomWidth: 1,borderColor: Global.Const.transparent,alignItems: 'center',flexDirection: 'row'}}>
                        <Text style={{marginLeft:8,fontSize:16,color:'black'}}>清除缓存</Text>
                        <View style={{flex: 1,alignItems: 'flex-end'}}>
                            <Text style={{marginRight:12,color:Global.FontColor.DarkGray}}>{this.state.cachesize}</Text>
                        </View>
                    </View>
                </TouchableOpacity>
                <TouchableOpacity activeOpacity={0.5}
                                  onPress={()=>this.LogOutAction(1)}>
                    <View
                        style={{backgroundColor:'white',width: Global.Const.size.width,height: 45,borderBottomWidth: 1,borderColor: Global.Const.transparent,alignItems: 'center',flexDirection: 'row'}}>
                        <Text style={{marginLeft:8,fontSize:16,color:'black'}}>关于我们</Text>
                        <View style={{flex: 1,alignItems: 'flex-end'}}>
                            <Image style={{ resizeMode:'contain',marginRight: 10,}}
                                   source={require('../../Image/icon_go.png')}></Image>
                        </View>
                    </View>
                </TouchableOpacity>
                <TouchableOpacity activeOpacity={0.5}
                                  onPress={()=>this.LogOutAction(2)}>
                    <View
                        style={{backgroundColor:'white',width: Global.Const.size.width,height: 45,borderBottomWidth: 1,borderColor: Global.Const.transparent,alignItems: 'center',flexDirection: 'row'}}>
                        <Text style={{marginLeft:8,fontSize:16,color:'black'}}>意见反馈</Text>
                        <View style={{flex: 1,alignItems: 'flex-end'}}>
                            <Image style={{ resizeMode:'contain',marginRight: 10,}}
                                   source={require('../../Image/icon_go.png')}></Image>
                        </View>
                    </View>
                </TouchableOpacity>
                <TouchableOpacity activeOpacity={0.5}
                                  onPress={()=>this.LogOutAction(3)}>
                    <View
                        style={{marginTop:150,backgroundColor:'#24CD78',borderRadius:5, marginLeft:10,marginRight:10,marginBottom:10,  borderWidth:1,borderColor: Global.Const.transparent,width:Global.Const.size.width-20,height:45, alignItems:'center',justifyContent:'center'}}>
                        <Text style={{color:'white'}}>退出登录</Text>
                    </View>
                </TouchableOpacity>
            </View>
        );
    }
}

var styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: Global.Const.transparent,
    },
});